package com.five.api.service.impl;

import com.five.api.controller.UserController;
import com.five.api.dao.CourseMapper;
import com.five.api.dao.OrdersMapper;
import com.five.api.service.CourseService;
import com.five.entity.Course;
import com.five.entity.Orders;
import com.five.entity.User;
import com.five.util.MySessionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author: star
 * @Date: 2021-05-21 14:01
 */
@Service
public class CourseServiceImpl implements CourseService {

    private Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private OrdersMapper ordersMapper;

    @Override
    public List<Course> findAll(Course course) {
        List<Course> courses = courseMapper.selAll(course);
        return courses;
    }

    @Override
    public List<Course> queryByCharge(long charge) {
        List<Course> courses = courseMapper.queryByCharge(charge);
        return courses;
    }

    @Override
    public List<Course> queryByCourseClassify(Long courseClassifyId) {
        List<Course> courses = courseMapper.queryByCourseClassify(courseClassifyId);
        return courses;
    }

    @Override
    public List<Course> queryByState(Long state) {
        List<Course> courses = courseMapper.queryByState(state);
        return courses;
    }

    @Override
    public Course queryById(HttpServletRequest request, Long courseId) {

        //判断用户是否登录，用户没登录，查询课程，用户以登录，该课程是否收费，如果收费，判断该用户是否以购买该课程，如果购买该课程，则返回该课程已免费
        MySessionContext mySc = MySessionContext.getInstance();

        Course course = courseMapper.queryById(courseId);

        if(null == request.getRequestedSessionId()){
            //当前用户未登录
            return course;
        }

        HttpSession session = mySc.getSession(request.getRequestedSessionId());

        if(null == session){
            //当前用户session已过期
            return course;
        }

        User user = null;
        try {
            user = (User) session.getAttribute("User");
        }catch (RuntimeException e){
            e.printStackTrace();
            return course;
        }

        if(null == user){
            //当前用户信息已过期
            return course;

        }

        //todo 判断该用户是否购买该课程
        Orders orders = ordersMapper.selectOrderByUserIdAndCourseId(user.getId(), courseId);
        if(orders == null || orders.getPayment() == null || 0 != orders.getPayment()){
            //该用户没有购买该课程,获该订单没有付款
            return course;
        }


        //用户已购买该课程,将课程对象的Charge设置该用户已购买（3）
        course.setCharge(3);
        return course;

    }
    @Override
    public List<Course> queryByViewCount() {
        List<Course> courses = courseMapper.queryByViewCount();
        return courses;
    }

    @Override
    public List<Course> queryByFuzzy(Course course) {
        List<Course> courses = courseMapper.queryByFuzzy(course);
        return courses;
    }

}
